package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.Order;
import com.juzi.wopu.domain.OrderResponse;
import com.juzi.wopu.domain.OrderStep;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.Type;
import com.juzi.wopu.query.OrderQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysCodeQuery;
import com.juzi.wopu.service.IOrderService;
import com.juzi.wopu.service.IOrderStepService;
import com.juzi.wopu.service.ISysCodeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class OrderAction extends CRUDAaction<Order> {
	
	private static final long serialVersionUID = 1L;

	private IOrderService orderService;
	
	private ISysCodeService sysCodeService;
	
	private IOrderStepService orderStepService;

	private Order order = new Order();
	
	private OrderQuery baseQuery = new OrderQuery();
	
	private PageList pageList;
	
	private String totalOrder;
	
	private List<OrderResponse> data=new ArrayList<>();
	
	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	public void setOrderStepService(IOrderStepService orderStepService) {
		this.orderStepService = orderStepService;
	}
	
	@Override
	protected void list() {
		List<Type> list=orderService.getAllType();
		Map<Long, SysCode> syscodeMap=new HashMap<>();
		Map<String, SysCode> namecodeMap=new HashMap<>();
		sysCodeService.getSysCodeMap(syscodeMap, namecodeMap);
		
		Map<Long, OrderStep> orderStepMap=new HashMap<>();
		orderStepService.getOrderStepMap(orderStepMap, null);
		
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		Long companyCode=namecodeMap.get(companyName).getiId();
		baseQuery.setCompanyCode(companyCode);
		
		//得到所有订单类型
		List<Type> typeList=new ArrayList<>();
		Map<String,Long> map=new HashMap<>();
		if(list!=null && list.size()>0){
			for(Type type:list){
				StringBuffer sb=new StringBuffer();
				StringBuffer sbCode=new StringBuffer();
				if(type.getTypeCode()!=null && syscodeMap.get(type.getTypeCode())!=null){
					map.put(syscodeMap.get(type.getTypeCode()).getsName(),type.getTypeCode());
					type.setTypeName(syscodeMap.get(type.getTypeCode()).getsName());
					sb.append(type.getTypeName());
					sbCode.append(type.getTypeCode());
					
					if(type.getSubTypeCode()!=null && syscodeMap.get(type.getSubTypeCode())!=null){
						type.setSubTypeName(syscodeMap.get(type.getSubTypeCode()).getsName());
						sb.append(" - ").append(type.getSubTypeName());
						sbCode.append(" - ").append(type.getSubTypeCode());
						type.setTotalTypeName(sb.toString());
						type.setTotalTypeCode(sbCode.toString());
						typeList.add(type);
					}
				}
			}
			
			for(String s:map.keySet()){
				Type type=new Type();
				type.setTypeCode(map.get(s));
				type.setTypeName(s);
				type.setSubTypeCode(null);
				type.setSubTypeName(null);
				type.setTotalTypeName(s);
				type.setTotalTypeCode(map.get(s)+"");
				typeList.add(type);
			}
		}
		putContext("typeList", typeList);
		
		//获得所有流程状态
		SysCodeQuery query=new SysCodeQuery();
		query.setiType(3L);
		List<SysCode> stepList = sysCodeService.find(query);
		putContext("stepList", stepList);
		
		this.pageList = orderService.findByQuery(baseQuery);
		totalOrder=pageList.getTotalcount()+"";
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			for(int i=0;i<pageList.getRows().size();i++){
				Order order=(Order) pageList.getRows().get(i);
				OrderResponse response=new OrderResponse(order);
				
				
				Long iStatus = order.getiStatus();
				SysCode sCode = syscodeMap.get(iStatus);
				if(iStatus==null || sCode==null){
					response.setStatusName("");
				}else{
					String sName = sCode.getsName();
					response.setStatusName(sName);
				}
				
				Long iCompany = order.getiCompany();
				SysCode sysCode = syscodeMap.get(iCompany);
				if(iCompany==null || sysCode==null){
					response.setComPanyName("");
				}else{
					String sName = sysCode.getsName();
					response.setComPanyName(sName);
				}
				
				Long iBuilding = order.getiBuilding();
				SysCode sysCode2 = syscodeMap.get(iBuilding);
				if(iBuilding==null || sysCode2==null){
					response.setBuildName("");
				}else{
					String sName = sysCode2.getsName();
					response.setBuildName(sName);
				}
				Long iFloor = order.getiFloor();
				SysCode sysCode3 = syscodeMap.get(iFloor);
				if(iFloor==null || sysCode3==null){
					response.setFloorName("");
				}else{
					String sName = sysCode3.getsName();
					response.setFloorName(sName);
				}
				
				Long iRoom = order.getiRoom();
				SysCode sysCode4 = syscodeMap.get(iRoom);
				if(iRoom==null || sysCode4==null){
					response.setRoomName("");
				}else{
					String sName = sysCode4.getsName();
					response.setRoomName(sName);
				}
				
				Long iType = order.getiType();
				SysCode sysCode5 = syscodeMap.get(iType);
				if(iType==null || sysCode5==null){
					response.setTypeName("");
				}
				else {
					String sName = sysCode5.getsName();
					response.setTypeName(sName);
				}
				
				Long iTypeSub = order.getiTypeSub();
				SysCode sysCode6 = syscodeMap.get(iTypeSub);
				if(iTypeSub==null || sysCode6==null){
					response.setTypeSubName("");
				}
				else {
					String sName = sysCode6.getsName();
					response.setTypeSubName(sName);
				}
				
				if(response.getTypeSubName().equals("")){
					response.setType(response.getTypeName());
				}else {
					response.setType(response.getTypeName()+" - "+response.getTypeSubName());
				}
				
				StringBuffer addr=new StringBuffer();
				if(!response.getComPanyName().equals("")){
					addr.append(response.getComPanyName());
					if(!response.getBuildName().equals("")){
						addr.append(" - ").append(response.getBuildName());
						if(!response.getFloorName().equals("")){
							addr.append(" - ").append(response.getFloorName());
							if(!response.getRoomName().equals("")){
								addr.append(" - ").append(response.getRoomName());
							}
						}
					}
				}
				response.setAddr(addr.toString()); 				
				data.add(response);
			}
		}
	}

	@Override
	public String input() throws Exception {
		
		return INPUT;
	}

	public void validateSave() {

	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				orderService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {
			order = orderService.get(id);
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			order = new Order();
		} else {
			order = orderService.get(id);
		}
	}

	@Override
	public Order getModel() {
		return order;
	}

	public PageList getPageList() {
		return pageList;
	}

	public OrderQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(OrderQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public List<OrderResponse> getData() {
		return data;
	}

	public void setData(List<OrderResponse> data) {
		this.data = data;
	}

	public String getTotalOrder() {
		return totalOrder;
	}

	public void setTotalOrder(String totalOrder) {
		this.totalOrder = totalOrder;
	}
}